<template>
  <div>
    <el-dialog
      :title="isAdd ? '添加商品规格' : '编辑商品规格'"
      :visible.sync="isShow"
      center
      :before-close="cancel"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          prop="specsname"
          label="规格名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          v-for="(item, index) in attrsArr"
          :key="index"
          label="规格属性"
          :label-width="formLabelWidth"
        >
          <div>
            <el-input
              class="inp"
              v-model="item.value"
              autocomplete="off"
            ></el-input>
            <el-button v-if="index == 0" type="primary" @click="addAttrs"
              >新增规格属性</el-button
            >
            <el-button v-else @click="del(index)">刪除</el-button>
          </div>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#409EFF"
            inactive-color="#DCDFE6"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { specsAdd, specsEdit, specsInfo } from "@/request/api";
import { mapActions } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      form: {
        specsname: "", //商品规格名称
        attrs: "", //商品规格属性值
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        specsname: [
          { required: true, message: "请输入商品规格名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
      attrsArr: [
        {
          value: "",
        },
      ],
    };
  },
  methods: {
    addAttrs() {
      if (this.attrsArr.length > 5) {
        this.$message.warning("最多只能添加5条");
      } else {
        this.attrsArr.push({
          value: "",
        });
      }
    },
    del(i) {
      this.attrsArr.splice(i, 1);
    },
    ...mapActions({
      getSpecs: "specs/changeSpecsListAction",
      getCount: "specs/changeCountAction",
    }),
    reset() {
      this.form = {
        specsname: "", //商品规格名称
        attrs: "", //商品规格属性值
        status: 1, //状态1正常2禁用
      };
      this.$refs.form.resetFields();
      this.attrsArr = [
        {
          value: "",
        },
      ];
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    comfirm() {
      this.$refs.form.validate((flag) => {
        if (flag) {
          this.form.attrs = this.attrsArr.map((item) => item.value).join(",");
          console.log(this.form);
          if (this.isAdd) {
            // console.log("添加");
            specsAdd(this.form).then((res) => {
              console.log(res, "商品规格添加");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getSpecs();
                this.getCount();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            // console.log("编辑");
            specsEdit(this.form).then((res) => {
              console.log(res, "商品规格编辑");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
               this.getSpecs();
                this.getCount();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          alert("验证失败");
        }
      });
    },
    lookup(id) {
      specsInfo({
        id,
      }).then((res) => {
        console.log(res, "获取一条数据");
        if (res.code == 200) {
          this.form = res.list[0];
          this.form.id = id;
          this.form.attrs.map((item, index) => {
            console.log(item, "每一项属性");
            if (index == 0) {
              this.attrsArr[0].value = item;
            } else {
              this.attrsArr.push({
                value: item,
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
.inp {
  width: 75%;
}
</style>
